<?php
namespace App\Services\Api;
use App\Models\Category;


class GoodsService
{
	public function getGoodsList($data = null)
	{
		$top_category = Category::top()->get();
		$category_id = !empty($data['category']) ? $data['category'] : $top_category[0]->id;
		
		$category = Category::findOrFail($category_id);
		$order = ['id', 'desc'];
		if(!empty($data['sort_field']) && !empty($data['order']))
		{
			$order = [$data['sort_field'], $data['order']];
		}
		//info($order);
		if($category->subcates()->exists())
		{
			return $category->subcates_goods()->orderBy($order[0], $order[1])->paginate();
		}
		return $category->goods()->orderBy($order[0], $order[1])->paginate();
		
	}

}